package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private JedisPool jedisPool;
    @Override
    public void add(Setmeal setmeal, Integer[] checkGroupIds) {
        //先添加该对象，并在sql中取出该id
        setMealDao.add(setmeal);
        //再导入套餐与检查组的关联，使用sql的批量导入
        setMealDao.setSetMealIdAndCheckGroupIds(setmeal.getId(),checkGroupIds);
        //将图片名称保存到redis
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setMealDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Setmeal findById(Integer id) {
        return setMealDao.findById(id);
    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        return setMealDao.findCheckGroupIdsBySetmealId(id);
    }

    @Override
    public void edit(Integer[] checkgroupIds, Setmeal setmeal) {
        //得到要修改的数据的id
        Integer id = setmeal.getId();
        //找到修改前的图片名称
        String imgName = setMealDao.findBysetmealId(id);
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,imgName);
        //修改表单数据
        setMealDao.edit(setmeal);
        //删除关联关系
        setMealDao.deleteAssocication(setmeal.getId());
        //建立关联关系
        setMealDao.setSetMealIdAndCheckGroupIds(setmeal.getId(),checkgroupIds);
        //将图片名称保存到redis
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    @Override
    public void delete(Integer id) {
        Integer count = setMealDao.findCountByOrder(id);
        if(count>0){
            throw new RuntimeException("该套餐已被预约，不能删除");
        }
        //删除套餐与检查组的关联表
        setMealDao.deleteAssocication(id);
        //根据id找到要删除的对象
        Setmeal setmeal = setMealDao.findById(id);
        //删除redis里对应的图片名称
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        //删除这个套餐
        setMealDao.delete(id);
    }

    @Override
    public List<Setmeal> findAllSetmeal() {
        return setMealDao.findAllSetmeal();
    }

    @Override
    public Setmeal findByIdSetmeal(int id) {
        return setMealDao.findByIdSetmeal(id);
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setMealDao.findSetmealCount();
    }
}
